package com.wuzuqing.android.eightdiagrams.term;

import com.wuzuqing.android.eightdiagrams.model.MeasureParams;
import com.wuzuqing.android.eightdiagrams.model.QiMenDunJiaItem;
import com.wuzuqing.android.eightdiagrams.model.QiMenDunJiaModel;
import com.wuzuqing.android.eightdiagrams.utils.DataUtils;
import com.wuzuqing.android.eightdiagrams.utils.EightDagramsManager;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description:
 * @Author: 吴祖清
 * @CreateDate: 29.1.24 10:34 AM
 */
public class Test {

    private static void saveCar() {
        // 纳音[白蜡金 城头土 天上火 桑柘木] 星期三 西方白虎 星宿[参水猿](吉) 彭祖百忌[戊不受田田主不祥 午不苫盖屋主更张] 喜神方位[巽](东南) 阳贵神方位[艮](东北) 阴贵神方位[坤](西南)
        // 福神方位[坎](正北) 财神方位[坎](正北) 冲[(壬子)鼠] 煞[北]
        Calendar calendar = Calendar.getInstance();
        calendar.set(2077, 0, 1, 0, 0, 1);
        FileOutputStream fileOutputStream = null;
        try {

            fileOutputStream = new FileOutputStream(System.getProperty("user.dir") + "/src/com/oxgen/gradle/term/a1" +
                    ".txt");
            Solar solar = Solar.fromDate(ExactDate.fromCal(calendar));
            int count = 0;
            while (count < 10000) {

                Lunar lunar = solar.getLunar();
                String text = solar + " " + lunar.toSimpleString();
                System.out.println(text);
                fileOutputStream.write(text.getBytes(StandardCharsets.UTF_8));
                fileOutputStream.write('\n');
                solar = solar.next(1, false);
//                calendar.add(Calendar.DATE, 1);
                count++;
            }
            fileOutputStream.flush();
            fileOutputStream.close();
            fileOutputStream = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void printSingle() {
        Solar solar = Solar.fromYmdHms(1991, 3, 26, 6, 29, 0);
        Lunar lunar = solar.getLunar();
        String text = solar + " " + lunar.toSimpleString();
        System.out.println(text);
        System.out.println(lunar.toFullString());
//        EightChar.TotalYun yun = lunar.getEightChar().getYun(1);
//        System.out.println(yun);
//        List<EightChar.DaYun> daYunList = yun.getDaYun(8);
//        for (EightChar.DaYun daYun : daYunList) {
//            System.out.println(daYun);
//            List<EightChar.LiuNian> liuNian = daYun.getLiuNian(10);
//            for (EightChar.LiuNian nian : liuNian) {
//                System.out.println(nian);
//                List<EightChar.LiuYue> liuYue = nian.getLiuYue();
//                StringBuilder sb = new StringBuilder();
//                for (EightChar.LiuYue yue : liuYue) {
//                    sb.append(yue).append("\t");
//                }
//                System.out.println(sb);
//            }
//        }
        List<String> baZi = lunar.getBaZi();
        System.out.println(baZi);
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
//        printSingle();
//        saveCar();
        ////222 1991-03-26 1991-02-04
        //// 333 1991-03-26 06:29:00 1991-02-04 16:08:24
//        String aa = "2024131|2024-05-20(一)|0,6,9";
//        System.out.println(aa.substring(8, 18));
        DataUtils.init();
        printFc3DOpenTime8Z();
//        Solar solar = Solar.fromYmdHms(2024, 7, 28, 8, 10, 0);
//        int count = 0;
//        while (count < 14) {
//            DataUtils.printQiMenBaGua(solar);
//            solar =Solar.fromDate(ExactDate.fromTimeInMillis(solar.getCalendar().getTime()+3600*2000));
//            count++;
//        }

        DataUtils.printQiMenBaGua(Solar.fromYmdHms(2007, 5, 18, 19, 20, 0),"阳4");
    }

    private static Map<String, String> readText() {
        //2024131|2024-05-20(一)|0,6,9
        Map<String, String> map = new HashMap<>();

        try (BufferedReader br = new BufferedReader(new InputStreamReader(DataUtils.openFile("fc3d.txt")))) {
            String line;
            while ((line = br.readLine()) != null) {
                map.put(line.substring(8, 18), line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    private static void printFc3DOpenTime8Z() {
        Lunar lunar = Solar.fromYmdHms(2024, 6, 7, 21, 15, 0).getLunar();
        String zhi1 = "甲|亥|子|丑|寅|卯|辰|巳|午|未|申|酉|戌";
        String zhi2 = "乙|午|巳|辰|卯|寅|丑|子|亥|戌|酉|申|未";
        String zhi3 = "丙|寅|卯|辰|巳|午|未|申|酉|戌|亥|子|丑";
        String zhi4 = "丁|酉|申|未|午|巳|辰|卯|寅|丑|子|亥|戌";
        String zhi5 = "戊|寅|卯|辰|巳|午|未|申|酉|戌|亥|子|丑";
        String zhi6 = "己|酉|申|未|午|巳|辰|卯|寅|丑|子|亥|戌";
        String zhi7 = "庚|巳|午|未|申|酉|戌|亥|子|丑|寅|卯|辰";
        String zhi8 = "辛|子|亥|戌|酉|申|未|午|巳|辰|卯|寅|丑";
        String zhi9 = "壬|申|酉|戌|亥|子|丑|寅|卯|辰|巳|午|未";
        String zhi10 = "癸|卯|寅|丑|子|亥|戌|酉|申|未|午|巳|辰";

        String[] Yun = new String[]{
                "长生", "沐浴", "冠带", "临高", "帝旺", "衰", "病", "死", "幕", "绝", "胎", "养"
        };
        String[] arr = new String[]{
                zhi1,
                zhi2,
                zhi3,
                zhi4,
                zhi5,
                zhi6,
                zhi7,
                zhi8,
                zhi9,
                zhi10
        };

        Map<String, List<String>> map = new HashMap<>();
        for (String str : arr) {
            String[] split = str.split("\\|");
            List<String> list = new ArrayList<>(Arrays.asList(split).subList(1, split.length));
            map.put(split[0], list);
        }

        Map<String, Integer> sLucyNumber = new HashMap<>();
        sLucyNumber.put("辰", 4);
        sLucyNumber.put("巳", 4);
        sLucyNumber.put("卯", 3);
        sLucyNumber.put("寅", 8);
        sLucyNumber.put("丑", 8);
        sLucyNumber.put("子", 1);
        sLucyNumber.put("亥", 6);
        sLucyNumber.put("戌", 6);
        sLucyNumber.put("酉", 7);
        sLucyNumber.put("申", 2);
        sLucyNumber.put("未", 2);
        sLucyNumber.put("午", 9);

        String[] Gan = new String[]{"甲", "乙", "丙", "丁", "戊", "己", "庚",
                "辛", "壬", "癸"};
        String[] GanWx = new String[]{"木", "木", "火", "火", "土", "土", "金",
                "金", "水", "水"};
        //木生火|木性温暖,火隐伏其中,钻木而生火,所以木生火.|火多木焚;强木得火,方化其顽.
        //火生土|火灼热,所以能够焚烧木,木被焚烧后就变成灰烬,灰即土,所以火生土.|土多火晦;强火得土,方止其焰
        //土生金|金需要隐藏在石里,依附着山,津润而生,聚土成山,有山必生石,所以土生金.|金多土变;强土得金,方制其壅
        //金生水|少阴之气(金气)温润流泽,金靠水生,销锻金也可变为水,所以金生水.|水多金沉;强金得水,方挫其锋
        //水生木|水温润而使树木生长出来,所以水生木.|木多水缩;强水得木,方泄其势
        //金克木|金属铸造的割切工具可锯毁树木.（有矿的土地不长草）|木多金缺;木弱逢金,必为砍折
        //木克土|树根吸收土中的营养,以补己用,树木强壮了,土壤如果得不到补充,自然削弱.|土多木折;土弱逢木,必为倾陷
        //土克水|土能防水.（兵来将挡水来土掩）|水多土流;水弱逢土,必为淤塞
        //水克火|火遇水便熄灭.|火多水干;火弱遇水,必然熄灭
        //火克金|烈火能融化金属.|金多火熄;金弱遇火,必见销熔


        Map<String, Integer> ganMap = new HashMap<>();
        Map<String, String> ganWxMap = new HashMap<>();
        Map<String, String> wxShengKeMap = new HashMap<>();
        wxShengKeMap.put("木火", "生");
        wxShengKeMap.put("火木", "生");
        wxShengKeMap.put("火土", "生");
        wxShengKeMap.put("土火", "生");
        wxShengKeMap.put("土金", "生");
        wxShengKeMap.put("金土", "生");
        wxShengKeMap.put("金水", "生");
        wxShengKeMap.put("水金", "生");
        wxShengKeMap.put("水木", "生");
        wxShengKeMap.put("木水", "生");
        wxShengKeMap.put("金木", "克");
        wxShengKeMap.put("木金", "克");
        wxShengKeMap.put("木土", "克");
        wxShengKeMap.put("土木", "克");
        wxShengKeMap.put("土水", "克");
        wxShengKeMap.put("水土", "克");
        wxShengKeMap.put("水火", "克");
        wxShengKeMap.put("火水", "克");
        wxShengKeMap.put("火金", "克");
        wxShengKeMap.put("金火", "克");
        wxShengKeMap.put("木木", "平");
        wxShengKeMap.put("金金", "平");
        wxShengKeMap.put("火火", "平");
        wxShengKeMap.put("水水", "平");
        wxShengKeMap.put("土土", "平");
        for (int i = 0; i < Gan.length; i++) {
            ganMap.put(Gan[i], i + 1);
            ganWxMap.put(Gan[i], GanWx[i]);
        }


        // 0 1 4 5 7 10 良辰
        //子午从 申起，丑未由 戌寻，寅申瞧 子上，卯西却在 寅；
        //辰戌把 辰数，已亥觅 午根。
        Map<String, Integer> liangChenMap = new HashMap<>();
        //23-0 1-2   3-4   5-6  7-8   9-10  11-12 13-14 15-16 17-18 19-20 21-22
        String[] Zhi = new String[]{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
        Map<String, Integer> zhiNumMap = new HashMap<>();
        for (int i = 0; i < Zhi.length; i++) {
            zhiNumMap.put(Zhi[i], i);
        }
        int[] lzArr = new int[]{0, 1, 4, 5, 7, 10};
        liangChenMap.put("子", 8);
        liangChenMap.put("午", 8);
        liangChenMap.put("丑", 10);
        liangChenMap.put("未", 10);
        liangChenMap.put("寅", 0);
        liangChenMap.put("申", 0);
        liangChenMap.put("卯", 2);
        liangChenMap.put("酉", 2);
        liangChenMap.put("辰", 4);
        liangChenMap.put("戌", 4);
        liangChenMap.put("巳", 6);
        liangChenMap.put("亥", 6);


        Map<String, String> readText = readText();
        int count = 0;
        StringBuilder sb = new StringBuilder();
        while (count < 2) {
            List<String> baZi = lunar.getBaZi();
            String shiGan = String.valueOf(baZi.get(3).charAt(0));
            List<String> list = map.get(shiGan);
            List<Integer> numList = new ArrayList<>();
            for (int i = 2; i < 5; i++) {
                numList.add(sLucyNumber.get(list.get(i)));
            }
            Solar solar = lunar.getSolar();
            //  // 年之天干、月日之数相加之和再除以8得到的余数作为
            //        // 年之天干、月日之数及时之地支数相加之和再除以8得到的余数作为下卦。
            //        // 　　10＋7＋1＝18 
            //        // 年之天干、月日之数及时之地支数相加之和再除以6得到的余数作为动爻。
            //        LunarUtil now = LunarUtil.getInstance().resetNow();
            //        measureText = now.getOldData();
            //        int topNumber = now.geTopNumber();
            //        int bottomNumber = topNumber + now.getHourZhi();
            //        startWork(topNumber, bottomNumber, getChangeYaoValue(topNumber + bottomNumber));


            String open = readText.get(solar.toYmd());
            String riGan = baZi.get(2).charAt(0) + "";
            String riGanWx = ganWxMap.get(riGan);
//            if (!"火".equals(riGanWx)){
//                count++;
//                lunar = lunar.next(-1);
//                continue;
//            }
            sb.setLength(0);
            sb.append(solar.toFullString()).append("\t").append(baZi).append("\t").append("日干:")
                    .append(riGan).append(riGanWx).append("\t");
            if (open != null) {
                String[] split = open.split("\\|");
                String[] openNum = split[split.length - 1].split(",");
                int sum = 0;
                for (String num : openNum) {
                    int index = (Integer.parseInt(num) + 9) % 10;
                    String openGan = Gan[index];
                    sb.append(num).append("=").append(openGan).append(ganWxMap.get(openGan))
                            .append(wxShengKeMap.get(riGanWx + ganWxMap.get(openGan)))
                            .append(" ,");
                    sum += Integer.parseInt(num);
                }
                sb.append(" ").append(sum).append("\t");
            } else {
                sb.append("open ").append(readText.get(solar.toYmd()));
            }
            QiMenDunJiaModel qiMenBaGua = DataUtils.printQiMenBaGua(solar);
            String riZhi = String.valueOf(baZi.get(2).charAt(1));
            Integer integer = liangChenMap.get(riZhi);
            for (int i : lzArr) {
                String zhi = Zhi[(i + integer) % 12];
                sb.append(zhi).append(zhiNumMap.get(zhi) + 1).append(",");
            }
            sb.append(numList);


            String nianZhi = String.valueOf(baZi.get(0).charAt(1));
            String yueZhi = String.valueOf(baZi.get(1).charAt(1));
            Integer nianShu = zhiNumMap.get(nianZhi) + 1;
            Integer yueShu = zhiNumMap.get(yueZhi) + 1;
            int topNum = nianShu + yueShu + lunar.getDay();
            int bottomNum = topNum + lunar.getTimeZhiIndex();
            EightDagramsManager.getInstance().startMeasure(MeasureParams.Companion.number(topNum, bottomNum, false));
            // 年之天干、月日之数相加之和再除以8得到的余数作为
            // 年之天干、月日之数及时之地支数相加之和再除以8得到的余数作为下卦。
            // 　　10＋7＋1＝18 
            // 年之天干、月日之数及时之地支数相加之和再除以6得到的余数作为动爻。

            //：子、寅、辰、午、申、成为阳支对应奇数：1、3、5、7、9、11
            //丑、卯、已、未、西、亥为阴支对应偶数：2、4、6、8、10、12
            //对甲午年十一月初二就可以这样起卦
            //上卦：年数十月数＋日数的和除8，求出余数，整除时取8数：
            //也就是：（7+11+2)）+8=2；余数是4，即上卦数为四。
            //下卦：年数+月数+日数＋时数的和除8，求出余数，整除时取8数：
            //即：（7+11+2+5）+8=3；余数是1，下卦数为一。
            //动交：年数+月数+日数+时数的和除6，出余数，整除时取6数：
            //就是： (7+11+2+5） +6=4；余数是1,1爻动。
            sb.append("\n");
            sb.append(EightDagramsManager.getInstance().getResult());
            System.out.println(sb);
            lunar = lunar.next(-1);
            count++;
        }
    }
}
